package com.noahplat.user.controller;

import com.noahplat.common.utils.Result;
import com.noahplat.user.pojo.Avatar;
import com.noahplat.user.service.AvatarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 头像关联操作
 *
 * @author 李东
 * @version 1.0.0
 * @Date: 2021/3/13 9:35
 **/
@RestController
@RequestMapping("/api/avatar")
public class AvatarController {

    @Autowired
    private AvatarService avatarService;

    /**
     * 根据stid查询头像列表
     *
     * @param stid
     * @return
     * @author 李东
     * @version 1.0.0
     * @Date: 2021/3/13 10:15
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:AvatarController:findAvatarList')")
    @GetMapping("/findAvatarList")
    public Result<List<Avatar>> findAvatarList(@Min(value = 1, message = "系统类型不能为空") Long stid) {
        return avatarService.findAvatarList(stid);
    }

    /**
     * 根据头像Id 查询头像信息
     *
     * @param id
     * @return
     * @author 李东
     * @version 1.0.0
     * @Date: 2021/3/13 10:25
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:AvatarController:findAvatarById')")
    @GetMapping("/findAvatarById")
    public Result<Avatar> findAvatarById(@Min(value = 1, message = "头像Id不能为空") Long id) {
        return avatarService.findAvatarById(id);
    }
    /**
     * 添加头像信息
     * @param avatar
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:AvatarController:addAvatar')")
    @PostMapping("/addAvatar")
    public Result addAvatar(@RequestBody Avatar avatar){
        return avatarService.addAvatar(avatar);
    }
    /**
     * 修改头像信息
     * @param avatar
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:AvatarController:updateAvatar')")
    @PutMapping("/updateAvatar")
    public Result updateAvatar(@RequestBody Avatar avatar){
        return avatarService.updateAvatar(avatar);
    }

    /**
     * 删除头像信息
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:AvatarController:delAvatar')")
    @DeleteMapping("/delAvatar")
    public Result delAvatar(@NotNull(message = "ID不能为空 ") @Min(value = 1, message = "Id不能为空")Long id){
        return avatarService.delAvatar(id);
    }

    /**
     * 分页查询信息
     * @param page
     * @param limit
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:AvatarController:getAvatars')")
    @GetMapping("/getAvatars")
    public Result getAvatars(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer limit){
        return avatarService.getAvatars(page, limit);
    }

}
